//
//  OnlineChattingVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/10.
//

#import "OnlineChattingVC.h"
#import <WebKit/WebKit.h>
@interface OnlineChattingVC ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic,strong) WKWebView * detailsView;
@end

@implementation OnlineChattingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViewLayout];
}

-(void)setSubViewLayout{
    
    [self.view addSubview:self.detailsView];
    [_detailsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(NavHeight, 0, 0, 0));
    }];
    
}

- (void) webView:(WKWebView *)webView
     requestMediaCapturePermissionForOrigin:(WKSecurityOrigin *)origin
     initiatedByFrame:(WKFrameInfo *)frame type:(WKMediaCaptureType)type
     decisionHandler:(void (^)(WKPermissionDecision decision))decisionHandlerii
     API_AVAILABLE(ios(15.0))
{
    decisionHandlerii(WKPermissionDecisionGrant);
}

-(WKWebView *)detailsView{
    
    if (!_detailsView) {
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        WKUserScript *script = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        [wkWebConfig.userContentController addUserScript:script];
        
        // 进行偏好设置
        WKPreferences *preferences = [WKPreferences new];
        preferences.javaScriptCanOpenWindowsAutomatically = YES;
        wkWebConfig.preferences = preferences;
        wkWebConfig.allowsInlineMediaPlayback = YES;
        
        _detailsView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:wkWebConfig];
        [_detailsView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.82:8081/index.html"]]];

        _detailsView.opaque = NO;
        _detailsView.backgroundColor = UIColor.whiteColor;
        _detailsView.scrollView.backgroundColor = UIColor.whiteColor;
        _detailsView.UIDelegate = self;
    
    }
    return _detailsView;
}
@end
